import { _decorator, Component, Label, Sprite, UI, sys } from 'cc';
import { SceneUtil } from '../../core_tgx/base/SceneUtils';
import { tgxUIMgr, tgxUIAlert, tgxSceneUtil } from '../../core_tgx/tgx';
import { GameMode } from '../../module_basic/scripts/GameMgr';
import { UserMgr } from '../../module_basic/scripts/UserMgr';
import { UI_SearchingRival } from '../../module_basic/ui_searching_rival/UI_SearchingRival';
import { ModuleDef } from '../../scripts/ModuleDef';
import { SceneDef } from '../../scripts/SceneDef';
import { GomokuGameMgr } from '../scripts/GomokuGameMgr';
import { UICreateRoom } from '../../module_basic/ui_create_room/UICreateRoom';
import { LobbyGameMgr } from '../../module_basic/scripts/LobbyGameMgr';
import { UIChat } from '../../module_basic/ui_chat/UIChat';
import { UIAnnouncement } from '../../module_basic/ui_announcement/UIAnnouncement';
const { ccclass, property } = _decorator;

@ccclass('GomokuLobbyScene')
export class GomokuLobbyScene extends Component {

    async start() {
        if (!UserMgr.inst.userId) {
            tgxSceneUtil.loadScene(SceneDef.START);
            return;
        }

        tgxUIMgr.inst.hideAll();

        let ret = await LobbyGameMgr.inst.connectToLobby();
        if(ret.isSucc){
            tgxUIMgr.inst.showUI(UIChat);
        }

        tgxUIMgr.inst.showUI(UIAnnouncement);
    }

    onBtnLobby() {
        SceneUtil.loadScene(SceneDef.LOBBY);
    }

    onBtnQuickPlayClicked() {
        tgxUIMgr.inst.showUI(UI_SearchingRival, (ui: UI_SearchingRival) => {
            this.scheduleOnce(async () => {
                let ret = await UserMgr.inst.rpc_QuickPlay('gomoku');
                ui.hide();
                if (!ret.isSucc) {
                    tgxUIAlert.show('未找到适合的对手');
                }
                else {
                    UserMgr.inst.doEnterSubGame({
                        ...ret.res,
                        uid: UserMgr.inst.uid,
                    });
                }
            }, 1.5);
        });
    }

    onBtnTraningModeClicked() {
        GomokuGameMgr.inst.initAIMode();
        tgxSceneUtil.loadScene(SceneDef.GOMOKU);
    }

    onBtnCreate() {
        tgxUIMgr.inst.showUI(UICreateRoom);
    }

    update(deltaTime: number) {

    }
}